package cn.com.simple.learning.juc;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;

/**
 * @author Jianxing.Jia
 * @date 2022/5/29
 * @description 描述信息
 */
public class CompletionServiceTest {
    public static void main(String[] args) throws InterruptedException {
        List<Future<Integer>> list = new ArrayList<>();
        ExecutorService executorService = new ThreadPoolExecutor(10, 20, 10_000L, TimeUnit.SECONDS, new LinkedBlockingQueue<>(150));
        CompletionService<Integer> completionService = new ExecutorCompletionService<>(executorService);

        Future<Integer> submit1 = completionService.submit(() -> {
            System.out.println(123);
            try {
                Thread.sleep(1_000);
            } catch (InterruptedException ignored) {
            }
        }, 123);
        Future<Integer> submit2 = completionService.submit(() -> System.out.println(456), 456);
        Future<Integer> submit3 = completionService.submit(() -> System.out.println(789), 789);
        list.add(submit1);
        list.add(submit2);
        list.add(submit3);

        list.forEach(x -> {
            try {
                System.out.println(x.get());
            } catch (InterruptedException | ExecutionException ignored) {
            }
        });


        completionService.poll();
    }
}
